package com.edison.demo.service.impl;

import com.edison.demo.entity.ConsumerBaseInfo;
import com.edison.demo.mapper.ConsumerBaseInfoMapper;
import com.edison.demo.service.BaseDataService;
import com.edison.demo.service.DataService;
import com.edison.demo.until.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
 * @author edison
 */
@Service
@Slf4j
public class ConsumerBaseInfoServiceImpl implements DataService<ConsumerBaseInfo> {


    private final ConsumerBaseInfoMapper consumerBaseInfoMapper;

    @Autowired
    public ConsumerBaseInfoServiceImpl(ConsumerBaseInfoMapper consumerBaseInfoMapper) {
        this.consumerBaseInfoMapper = consumerBaseInfoMapper;
    }


    @Override
    @Async
    public CompletableFuture<List<ConsumerBaseInfo>> initData(int number) {
        log.info("trying to create mock consumerBaseInfo...");
        return BaseDataService.initData(number, this::initConsumerBaseInfo)
                .thenApply(consumerBaseInfos -> {
                    log.info("Finished creating consumerBaseInfos, number: {}", consumerBaseInfos.size());
                    return consumerBaseInfos;
                });
    }

    @Override
    @Async
    public CompletableFuture<Integer> batchInsert(List<ConsumerBaseInfo> list) {
        return BaseDataService.batchInsert(list,3000,consumerBaseInfoMapper::insert);
    }


    private ConsumerBaseInfo initConsumerBaseInfo(){
        ConsumerBaseInfo consumerBaseInfo = new ConsumerBaseInfo();
        consumerBaseInfo.setIdCardNumber(ChineseIDCardNumberGenerator.generate());
        consumerBaseInfo.setCustomerNumber(CustomerNumberGenerator.generateCustomerNumber());
        consumerBaseInfo.setCustomerName(ChineseNameGenerator.generateChineseName());
        consumerBaseInfo.setDocNum(DocumentNumberGenerator.generateDocumentNumber());
        consumerBaseInfo.setCustomerManagerNumber(CustomerManagerNumberGenerator.generateCustomerManagerNumber());
        // TBC this value look like customerManagerNumber, will set as customerManagerNumber
        consumerBaseInfo.setOpenAcctOrg(CustomerManagerNumberGenerator.generateCustomerManagerNumber());
        consumerBaseInfo.setCustomerType((Math.random() < 0.5) ? "1" : "2");
        consumerBaseInfo.setDocTp(DocTypeGenerator.generateDocType());
        consumerBaseInfo.setCustomerManagerName(ChineseNameGenerator.generateChineseName());

        return consumerBaseInfo;
    }
}
